export const startKm = 2;
export const longKm = 8;

export const startPrice = 6;
export const everyPrice = 0.8;
export const waitPrice = 0.25;
export const longMuti = 0.5;

export const fixNum = f => f * 100;
export const reNum = n => n / 100;

export const longPrice = reNum(fixNum(everyPrice) * (1 + longMuti));


export const isInStart = k => k <= startKm;
export const isInNormal = k => k > startKm && k <= longKm;
export const isInLong = k => k > longKm;

export const roundResult = k => Math.round(k);

export const cacuNormal = k => reNum(fixNum(k - startKm) * everyPrice);

export const cacuLong = k => reNum(fixNum(k - longKm) * longPrice);

export const cacuBase = t => startPrice + t * waitPrice;

export const caculate = (k, t = 0) => {
  let result = cacuBase(t);
  if (isInNormal(k)) {
    result += cacuNormal(k);
  }
  if (isInLong(k)) {
    result += cacuNormal(longKm) + cacuLong(k);
  }
  return roundResult(result);
};

export const rgxExec = line => {
  const rgx = /(\d+)公里,等待(\d+)分钟/g; 
  return rgx.exec(line);
};

export const formatResult = p => '收费' + p + '元';

export const mapTaxiFee = line => {
  const rs = rgxExec(line);
  if (rs && rs.length === 3) {
    const p = caculate(rs[1], rs[2]);
    return formatResult(p);
  } else {
    return '不正确的输入';
  }
};